package org.example.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.JavaBean.plus.Emp;
import org.example.service.EmpService;
import org.example.util.JsonResult;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Auther:Liu
 * @Testname:App
 * @Date:2025/8/4 9:40
 */
@Slf4j
@SpringBootTest
public class App {

    @Resource
    EmpService empService;

    @Test
    public void a() {
        List<Emp> list = empService.list();
        log.info("list:{}", list);
    }

    @Test
    public void b() {
        Emp emp = new Emp();
        emp.setEname("赵六");
        emp.setJob("java开发师");
        emp.setComm(89.99);
        emp.setDeptno(30);
        empService.save(emp);
    }

    @Test
    public void c() {
        boolean b = empService.removeById(7839);
        log.info("b:{}", b);
    }

    @Test
    public void d() {
        Emp emp = new Emp();
        emp.setEname("韦新悦");
        emp.setComm(198.9);
        emp.setSal(9929.9);
        emp.setJob("网页设计");
        emp.setDeptno(30);
        emp.setEmpno(8043);
        empService.updateById(emp);
    }

    @Test
    public void e() {
        List<Integer> list = Arrays.asList(345, 454, 343);
        empService.removeBatchByIds(list);
    }

    @Test
    public void f() {
        PageHelper.startPage(1, 10);
        List<Emp> list = empService.list();
        PageInfo<Emp> empPageInfo = new PageInfo<>(list);
        log.info("empPageInfo:{}", empPageInfo);
    }

    @Test
    public void g1() {
        List<Map<String, Object>> maps = empService.listMaps();
        log.info("maps:{}", maps);
    }

    @Test
    public void g2() {
        Emp byId = empService.getById(8090);
        log.info("byId:{}", byId);
    }

    @Test
    public void g3() {
        List<Object> objects = empService.listObjs();
        log.info("objects:{}", objects);
    }

    @Test
    public void g4() {
        List<Emp> emps = empService.listByIds(Arrays.asList(8090, 8041, 8043));
        log.info("emps:{}", emps);
    }

    @Test
    public void h1() {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
//        =  >  <  >=  <=  !=
//        wrapper.eq("job","程序员"); // =
//        wrapper.ne("job","程序员");  //  !=
//        wrapper.gt("sal","8000");  //>
//        wrapper.ge("empno",8043); //  >=
//        wrapper.lt("sal","2000"); // <
        wrapper.le("sal", "2000"); //  <=
        List<Emp> list = empService.list(wrapper);
        for (Emp emp : list) {
            log.info("emp:{}", emp);
        }
    }

    @Test
//    模糊查询
    public void h2() {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
//        wrapper.like("ename","张");
//        wrapper.notLike("ename","张");
//        wrapper.likeLeft("ename","张");
        wrapper.likeRight("ename", "张");
        List<Emp> list = empService.list(wrapper);
        for (Emp emp : list) {
            log.info("emp:{}", emp);
        }
    }

    @Test
//    范围查询
    public void h3() {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
//        wrapper.between("sal","2000","4000");
//        wrapper.notBetween("sal","2000","4000");
//        wrapper.in("job",Arrays.asList("a","b","c"));
        wrapper.notIn("job", Arrays.asList("a", "b", "c"));
        List<Emp> list = empService.list(wrapper);
        for (Emp emp : list) {
            log.info("emp:{}", emp);
        }
    }

    @Test
//    排序
    public void h4() {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
//        wrapper.orderByAsc("sal"); //升序
//        wrapper.orderByDesc("sal"); //降序
        wrapper.orderByDesc("sal", "job");
        List<Emp> list = empService.list(wrapper);
        for (Emp emp : list) {
            log.info("emp:{}", emp);
        }
    }

    @Test
//    分组
    public void h5() {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.groupBy("job");
        wrapper.select("job", "count(0)", "max(0)", "min(0)");
        wrapper.having("count(0)>0");
        List<Emp> list = empService.list(wrapper);
        for (Emp emp : list) {
            log.info("emp:{}", emp);
        }
    }

    @Test
//    拼接条件
    public void h6() {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.eq("sal", "2000").or().gt("sal","18800");
        List<Emp> list = empService.list(wrapper);
        for (Emp emp : list) {
            log.info("emp:{}", emp);
        }
    }

    @Test
    public void h7() {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.last("limit 1");
//        wrapper.first("limit 1");
        List<Emp> list = empService.list(wrapper);
        for (Emp emp : list) {
            log.info("emp:{}", emp);
        }
    }

    @Test
    public void h8() {
        UpdateWrapper<Emp> wrapper = new UpdateWrapper<>();
        wrapper.set("sal","2000");
        wrapper.set("job","程序开发师");
        wrapper.eq("empno","8056");
        List<Emp> list = empService.list(wrapper);
        for (Emp emp : list) {
            log.info("emp:{}", emp);
        }
    }

    @Test
    public void i() {
        JsonResult all = empService.findAll();
        log.info("all:{}", all);
    }
}
